package com.stylefeng.guns.rest.modular.content.controller;

import com.stylefeng.guns.rest.common.CommonResponse;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.BussinessException;
import com.stylefeng.guns.rest.common.persistence.model.Article;
import com.stylefeng.guns.rest.common.persistence.model.Content;
import com.stylefeng.guns.rest.common.persistence.model.FileEntity;
import com.stylefeng.guns.rest.common.persistence.model.Version;
import com.stylefeng.guns.rest.common.utils.TextUtil;
import com.stylefeng.guns.rest.common.utils.aliyunoss.OSSUploadUtil;
import com.stylefeng.guns.rest.modular.artitle.controller.dto.ArticleDetailResponse;
import com.stylefeng.guns.rest.modular.artitle.service.ArticleService;
import com.stylefeng.guns.rest.modular.artitle.service.FileService;
import com.stylefeng.guns.rest.modular.attent.controller.dto.AttentHotRequest;
import com.stylefeng.guns.rest.modular.content.controller.dto.*;
import com.stylefeng.guns.rest.modular.content.service.ContentService;
import com.stylefeng.guns.rest.modular.version.controller.dto.VersionRequest;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * Created by ASUS-PC on 2018/6/26.
 */
@RestController
@RequestMapping("/content")
public class ContentController {
    private final Log logger = org.apache.commons.logging.LogFactory.getLog(this.getClass());

    @Autowired
    ContentService contentService;
    @Autowired
    ArticleService articleService;

    //添加评论
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public ResponseEntity<?> queryVersionInfo(@RequestBody AddContentRequest request) {
        if(TextUtil.isEmpty(request.getContent())||
                request.getUserId()==0||
                request.getTitleId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        Article a= articleService.queryArtitle(request.getTitleId());
        Content content = new Content();
        content.setUserId(request.getUserId());
        content.setTitleId(request.getTitleId());
        content.setTitleUser(a.getUserId());
        content.setContent(request.getContent());
        content.setCreateTime(new Date());
        if(request.getParentId()>0){
            content.setParentId(request.getParentId());
        }
        int state = contentService.insertContent(content);
        if(state>0){
            return ResponseEntity.ok(CommonResponse.ok(null));
        }
        throw new BussinessException(BizExceptionEnum.UNKNOWN_ERROR);
    }

    //查询文章的评论
    @RequestMapping(value = "/commentList", method = RequestMethod.POST)
    public ResponseEntity<?> queryVersionInfo(@RequestBody CommentListRequest request) {
        if(request.getTitleId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        ContentData contentData = new ContentData();
        //查询评论总数
        int counts = contentService.getCommentAllNums(request.getTitleId());
        //查询评论
        List<CommentListResponse> lists = contentService.getTitleCommentList(request);
        //查询是否有父评论内容
        if(lists!=null && lists.size()>0){
            for (int i = 0; i < lists.size(); i++) {
                CommentListResponse response = lists.get(i);
                if(response.getParentId()>0){
                    ParentComment p = contentService.getTitleCommentById(response.getParentId());
                    response.setParentComment(p);
                }else{
                    response.setParentComment(new ParentComment());
                }
            }
        }
        contentData.setAllNums(counts);
        contentData.setLists(lists);
        return ResponseEntity.ok(CommonResponse.ok(contentData));
    }


    //获取我的评论
    @RequestMapping(value = "/myCommentList", method = RequestMethod.POST)
    public ResponseEntity<?> queryVersionInfo(@RequestBody AttentHotRequest request) {
        if(request.getUserId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        return ResponseEntity.ok(CommonResponse.ok(contentService.getMyCommentList(request)));

    }

    //获取评论的回复
    @RequestMapping(value = "/replyCommentList", method = RequestMethod.POST)
    public ResponseEntity<?> queryReplyCommentList(@RequestBody AttentHotRequest request) {
        if(request.getUserId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        return ResponseEntity.ok(CommonResponse.ok(contentService.getReplyMyCommentList(request)));

    }


    //删除评论
    @RequestMapping(value = "/deleteCommentById", method = RequestMethod.POST)
    public ResponseEntity<?> deleteCommentBy(@RequestBody CommentDeleteRequest request) {
        if(request.getId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        boolean state = contentService.deletCommentById(request.getId());
        if(state){
            return ResponseEntity.ok(CommonResponse.ok("操作成功" , null));
        }
        throw new BussinessException(BizExceptionEnum.DELET_FAUIL);
    }


}
